package com.yaowk.system.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.JMap;
import com.yaowk.authc.model.Role;
import com.yaowk.authc.model.User;
import com.yaowk.authc.model.UserPermission;
import com.yaowk.common.kit.CollectionKit;
import com.yaowk.common.kit.ResultKit;
import com.yaowk.system.model.Column;
import com.yaowk.system.model.Module;
import org.apache.shiro.SecurityUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 其它
 *
 * @author yaowk
 *         2017-4-19
 */
public class SystemController extends Controller {

    /**
     * @title api文档入口
     */
    public void api() {
        render("../../../../system/src/test/system.html");
    }

    /**
     * 获取模块配置
     *
     * @title 获取模块配置信息
     */
    public void config() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        Set<String> moduleAccepts = new HashSet<>();
        List<Role> roleList = Role.dao.findListByUserId(user.getId());
        List<UserPermission> userPermissionList = UserPermission.dao.findByUserId(user.getId());
        if (CollectionKit.isNotEmpty(roleList)) {
            for (Role role : roleList) {
                List<UserPermission> rolePermissionList = UserPermission.dao.findByRoleId(role.getId());
                if (CollectionKit.isNotEmpty(rolePermissionList)) {
                    userPermissionList.addAll(rolePermissionList);
                }
            }
        }
        if (userPermissionList != null) {
            for (UserPermission userPermission : userPermissionList) {
                String sign = userPermission.getStr("sign");
                String[] module = sign.split(":");
                moduleAccepts.add(module[0]);
            }
        }

        List<Module> moduleList = Module.dao.findByPlatformId(user.getPlatformId());
        // modules集合
        List<JMap> modules = new ArrayList<>();
        for (Module module : moduleList) {
            if (!moduleAccepts.contains(module.getType())) {
                continue;
            }

            // 封装module
            JMap moduleMap = JMap.create();
            if (module.getOpen() == 1) {
                // 封装menu
                List<JMap> columns = new ArrayList<>();
                List<Column> columnList = Column.dao.findByModuleId(module.getId());
                for (Column column : columnList) {
                    if (column.getOpen() == 1) {

                        JMap jMap = JMap.create();
                        jMap.set("code", column.getSign())
                                .set("name", column.getName());
                        columns.add(jMap);
                    }
                }
                moduleMap.set("menus", columns)
                        .set("id", module.getId())
                        .set("sort", module.getSort())
                        .set("code", module.getSign())
                        .set("name", module.getName())
                        .set("icon", module.getIcon());
                if (module.getEditColumn() == 1) {
                    moduleMap.set("editColumn", true);
                }
                else {
                    moduleMap.set("editColumn", false);
                }
                modules.add(moduleMap);
            }
        }
        renderJson(ResultKit.success(modules));
    }
}
